package chai.ningning.schedule;

public class YieldTest {

    static class YieldThread extends Thread{

        private boolean yield;

        public YieldThread(String name,int priority, boolean yield) {
            super(name);
            this.setPriority(priority);
            this.yield = yield;
        }

        @Override
        public void run() {
            Thread t = Thread.currentThread();
            for (int i = 0; i < 5; i++) {
                System.out.println(t+" : "+i);
                if (yield){
                    //线程让位：给与当前线程同等优先级或优先级高于当前线程的线程让位
                    Thread.yield();
                }
            }
        }
    }

    public static void main(String[] args) {

        Thread first = new YieldThread( "First" , Thread.NORM_PRIORITY - 2 , false );
        Thread second = new YieldThread( "Second" , Thread.NORM_PRIORITY , true );
        Thread third = new YieldThread("Thread", Thread.NORM_PRIORITY + 2 , false );

        first.start();
        second.start();
        third.start();
    }
}
